﻿package ui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;

import generalManagerLogic.MockGeneralManagerLogic;

import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

import main.Main;

import vo.generalManager.LoginPanelVO;
import vo.generalManager.TicketPanelVO;

import components.MyButton;
import components.MyFrame;
import components.MyPanel;


public class TicketPanel extends MyPanel {
	
	private JLabel userlistLabel;
	private JLabel searchLabel;
	private DefaultTableModel dtm;
	private JTable userlist;
	private JScrollPane jsp;
	private JTextField searchField;
	private JLabel classLabel;
	private JComboBox classComboBox;
	private MyButton searchButton;
	private MyButton giveButton;
	
	private MyButton logoutButton;
	private MyButton returnbackButton;

	protected void initComponent() {
		try {
			Main.getLogic().searchCustomer();
		} catch (RemoteException e1) {
			e1.printStackTrace();
		}
		
		TicketPanelVO tpvo=null;
		try {
			tpvo=Main.getLogic().getTpvo();
		} catch (RemoteException e) {
			e.printStackTrace();
		}
		
		logoutButton = new MyButton("注销");
		returnbackButton = new MyButton("返回");
		logoutButton.setBounds(MyFrame.WIDTH-80,0, 80, 20);
		returnbackButton.setBounds(MyFrame.WIDTH-160,0, 80, 20);
		this.add(logoutButton);
		this.add(returnbackButton);
		
		this.setBounds(300, 150, 700, 450);
		userlistLabel = new JLabel("用户列表：");
		searchLabel = new JLabel("精确查找：");
		dtm = new DefaultTableModel(tpvo.getUserData(),new String[]{"用户名","身份"});
		userlist = new JTable(dtm);
		DefaultTableCellRenderer tcr = new DefaultTableCellRenderer();// 设置table内容居中
		tcr.setHorizontalAlignment(SwingConstants.CENTER);
		userlist.setDefaultRenderer(Object.class, tcr);
		jsp = new JScrollPane(userlist);
		jsp.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		searchField = new JTextField();
		searchField.setText((tpvo.getKeyword()==null)?"请输入关键字":tpvo.getKeyword());
		classLabel = new JLabel("类别：");
		classComboBox = new JComboBox(new String[]{"用户名"});
		classComboBox.setSelectedIndex(tpvo.getKeywordType());
		searchButton = new MyButton("精确查找");
		giveButton = new MyButton("赠送优惠劵");
		userlistLabel.setBounds(10, 20, 100, 20);
		searchLabel.setBounds(400, 20, 100, 20);
		jsp.setBounds(10, 40, 350, 300);
		searchField.setBounds(390, 50, 200, 25);
		classLabel.setBounds(390, 100, 80, 20);
		classComboBox.setBounds(490, 100, 100, 20);
		searchButton.setBounds(480, 200, 100, 40);
		giveButton.setBounds(480, 300, 100, 40);
		this.add(jsp);
		this.add(userlistLabel);
		this.add(searchLabel);
		this.add(searchField);
		this.add(classLabel);
		this.add(classComboBox);
		this.add(searchButton);
		this.add(giveButton);
		
		logoutButton.setButtonAction("logout");
		returnbackButton.setButtonAction("toMain");
		searchButton.setButtonAction("searchCustomer");
		giveButton.setButtonAction("toGive");
	}

	protected void updateVO() {
		TicketPanelVO tpvo=null;
		try {
			tpvo = Main.getLogic().getTpvo();
		} catch (RemoteException e) {
			e.printStackTrace();
		}
		tpvo.setKeyword(searchField.getText());
		tpvo.setKeywordType(classComboBox.getSelectedIndex());
		tpvo.setSelectedRow(userlist.getSelectedRow());
		try {
			Main.getLogic().setTpvo(tpvo);
		} catch (RemoteException e) {
			e.printStackTrace();
		}
	}
}
